$(function (){
	//搜索
	var searchKey = $(".searchKey");
	
	if( searchKey.length > 0 ){
			
		searchKey.blur();//浏览器后退按钮有时候会激活此输入框document.activeElement == searchKey.get(0)用于重置输入框状态
		
		if( $.trim(searchKey.val()).length > 0 /*|| document.activeElement == searchKey.get(0)*/){
			searchKey.css( 'background-position', '0 -25px' );
		}
		
		searchKey.focusin(function(){
			var emptyInput = $.trim($(this).val()).length <= 0;
			if(emptyInput){
				$( this ).css( 'background-position', '0 -25px' );//无文字的背景
			}
		}).focusout(function(){
			var emptyInput = $.trim($(this).val()).length <= 0;
			if(emptyInput){
				$( this ).css( 'background-position', '0 0px' );//有文字的背景
			}
		});
		
		$("#searchBtn").click(function(){
			if( $.trim($(".searchKey").val()).length </*=*/ 0 ){
				$(".searchKey").focus();
				$(".searchKey").trigger("focusin");//当前版本jQuery需要手动触发
				return false;
			}else{
				$("#searchForm").submit();
				return true;
			}
		});
	}
	
	$(function(){
		$(".toggleautoctrl").click(function(){
			var list = $(this).next();
			list.toggle();
			var arrow = $(this).find("a:last-child");
			arrow.get(0).className = "icon "+( list.css("display")=="none"?"down":"up");
			if(arrow.hasClass("down")){
				arrow.attr("title","展开");
			}else{
				arrow.attr("title","收起");
			}
		});
	});

//插入个人信息卡
var uInfoCard= [];
function appendCardHtml(){
	if(typeof appendCardHtml._initialized== "undefined")//ajax只执行一次
	{
		$('body').append('<div class="uCard " style="display:none; "></div>')

		$(".uCard").bind({
			mouseenter: function() {
				$(".uCard").show();
			},	mouseleave: function() {
				$(".uCard").hide()
			}
		})
		appendCardHtml._initialized=true
	}
}

//根据数据返回html
function setCardHtml( fdata)
{
	cardHtml=''
	if(fdata.data)
	{
		uinfo = fdata.data
		if(uinfo.name== uinfo.myname){
			cardHtml = '<div class="tip" style="display: block;">这是你自己</div><div class="tip" style="display: block;"></div>'
		}else{
			cardHtml = '<div class="tip" style="display: none;">这是你自己</div><div class="tip" style="display: none;"></div>'
		}
		
		cardHtml += (uinfo.head.length>0)? '<div class="userPic"><a href="index.php?m=guest&u='+uinfo.name+'"  class="userPicA"><img src="'+uinfo.head+'/50"></a>':'<div class="userPic"><a href="index.php?m=guest&u='+uinfo.name+'"  class="userPicA"><img src="style/images/head_50.jpg"></a>';
		cardHtml += uinfo.Ismyidol? '<p class="btn"><input type="button" value="" class="addAttention" rel="'+uinfo.name+'" style="display: none;"><a title="取消收听" class="delAttention"  rel="'+uinfo.name+'"  href="javascript:;" style="display: block;">取消</a></p>': '<p class="btn"><input type="button" value="" class="addAttention" style="display: block;"  rel="'+uinfo.name+'" ><a title="取消收听" class="delAttention"  href="#" style="display:none ;"  rel="'+uinfo.name+'" >取消</a></p>';//是否为我收听的人
		cardHtml += '</div><div class="uCardcnt"><p class="userName"><a href="index.php?m=guest&u='+uinfo.name+'">'+uinfo.nick+'</a>';
		if(uinfo.isvip) 	cardHtml +='<span class="renzheng"></span>';
		cardHtml +='<br><span class="en">@'+uinfo.nick+'</span></p><p class="nums">广播'+uinfo.tweetnum+'条<span>|</span>听众'+uinfo.fansnum+'人</p><p class="btn"></p>';
		//<a class="note" href="#" account="doraworld" chname="'+uinfo.nick+'" nick="'+uinfo.nick+'">备注</a><a class="reply" href="#">对话</a><a class="msg    disabled" href="#" title="私信只能发给你的听众">私信</a><a class="black" href="#" rel="{u:\'doraworld\',name:\'哆啦A梦欢乐世界\',sex:\'它\'}">拉黑</a> todo
		cardHtml +='<div class="uInfobox"><div class="sepline"></div>'
		if(uinfo.introduction.length){
			if(uinfo.introduction.length>36)
			{
				introduction_short = uinfo.introduction.substr(0,36)+'...'
			}else{
				introduction_short = uinfo.introduction
			}
			cardHtml +='<p class="uIntro" style="display: block;" title="'+uinfo.introduction+'">'+introduction_short+'</p>'
		}
		
		if(uinfo.tag)
		{
			cardHtml += '<p class="tagBox" style="display: block;">标签：';
			taglen = (uinfo.tag.length>10) ? 9: uinfo.tag.length-1
			for(i=0;i<taglen;i++){
			cardHtml +='<span class="last line"><a href="javascript:;">'+uinfo.tag[i].name+'</a></span>'
			}
			if(uinfo.tag.length > 10) cardHtml +='...'
			cardHtml += '</p>';
		}
		cardHtml +='<p class="company" style="display: none;"></p><p class="school" style="display: none;"></p></div><a  style="display: none;" class="ico_sFo" href="#" title="添加到特别收听"></a></div><div class="uloadBox"  style="display: none;"><em class="loading"></em>资料卡加载中</div><div class="SA"><em>◆</em><span>◆</span><b>◆</b></div>';
	}
	return cardHtml;

}



	//鼠标滑过头像 显示个人信息提示
	$('.ttouxiang >a, .utouxiang >a, .headmodel >a').live({
		mouseenter: function() {
			appendCardHtml();
			uname = $(this).attr('href')
			uname = uname.substring(uname.lastIndexOf ('u=')+2, uname.length)//用url里得到用户名
			if(!uname) return
			if(!uInfoCard[uname])//检查用户信息缓存，存在就不再取
			{
				//采用了同步方式，减少多次mousemove可能拉取多次同一数据
				$.ajax({type:"GET",url:'index.php',data:{m:'a_guestinfo',u:uname},cache: true,async:false,success:function(data){
							try{
								guestInfo = eval('('+data+')')
								if(!data.code)
								{
									cardHtml =  setCardHtml(guestInfo.data)
									$(".uCard").html(cardHtml)
									uInfoCard[uinfo.name] = guestInfo.data
									$(".uCard").show()
								}else{
									alert(data.msg)
								 }
							}catch(e){}

					}
				})
			}else{
					cardHtml =  setCardHtml(uInfoCard[uname])
					$(".uCard").html(cardHtml)
					$(".uCard").show()
			}
			//重订位置
			ioffset = $(this).offset();
			$(".uCard").css({top:ioffset.top+'px', left:ioffset.left+'px'})

			//增加收听
			$('.addAttention').bind('click',function(e){
				$.post('index.php?m=a_follow',{name:uname,type:1},function(data){
						try{
							data = eval('('+data+')')
							if(data.code==0)
							{
								$('.delAttention[rel='+uname+']').show()
								$('.addAttention[rel='+uname+']').hide()
								guestInfo.data.data.Ismyidol=1
								uInfoCard[uinfo.name] = guestInfo.data
							}
						}catch(e){}
				})
				return false;
			})

			//取消收听
			$('.delAttention').bind('click',function(e){
					$.post('index.php?m=a_follow',{name:uname,type:0},function(data){
						try{
							data = eval('('+data+')')
							if(data.code==0)
							{
								$('.addAttention[rel='+uname+']').show()
								$('.delAttention[rel='+uname+']').hide()
								guestInfo.data.data.Ismyidol=0
								uInfoCard[uinfo.name] = guestInfo.data
							}
						}catch(e){}
				})
					return false;
			})
		}

	});

});
